﻿global DESKIDX := 1
global NUMBER_TOGGLE := 0

Control::Click

#Enter::
    Run, wt
return

#q::
    if WinExist("ahk_exe msedge.exe")
        WinActivate ; Use the window found by WinExist.
    else
        Send, #1
return

#w::
    Send, #2
return

#e::
    Send, #3
return

#r::
    Send, #4
return

^l::
    Send, {shift} ; 中文输入法英文上字
    Send, #{Space}
return

; Esc 相关快捷键
Esc::Escape
Escape::Escape
; Escape::send, Escape
!Escape::
; qwertyuiop
Esc & q::call_first_edge()
Esc & w::#2
Esc & e::call_first_vscode()
Esc & r::call_wechat()
Esc & t::Send, !^x
Escape & t::Send, !^x
Esc & y::Esc
Esc & u::Send, !{F4}
Esc & i::Esc
Esc & o::Esc
Esc & p::Esc

Esc & a::reload
Esc & c::Send, ^c
Esc & s::#l

; Esc & e::Send, {Win down}3
Esc & g::#d
Esc & j::call_first_edge()
Esc & k::#2
Esc & l::call_first_vscode()
; Esc & h::#4
Esc & h::Esc
^h::Click
; ^i::Send, {Blind}{Up}
; ^j::Send, {Blind}{Left}
; ^k::Send, {Blind}{Down}
; ^l::Send, {Blind}{Right}
^i::Send, {Up}
; ^n::Send, {Left}
^k::Send, {Down}
; ^i::Send, {Right}
Esc & m::Esc
return

; Esc & k::call_alacritty()
Esc & n::#6
; Esc & f::#6
Esc & f::send, ^!x


Esc & `;::Esc


Esc & .::
test()
return

Join(sep, params*) {
    for index,param in params
        str .= param . sep
    return SubStr(str, 1, -StrLen(sep))
}

test() {
    ; MsgBox % Join("`n", "one", "two", "three")
    run, Fluent Terminal
}

Esc & v::Send, ^v

Esc & x::Esc
; Esc & j::Esc
; Esc & z::Esc
Esc & z::Send, !^i
Esc & tab::Send, ^{tab}
; Esc & Space::Send, #{Up}
Esc & Space::
wingetpos, X,Y,Width,Height, A
if(Width >= 2500 and Height >= 1350){
    send, #{Down}
}else{
    send, #{Up}
}
return

Esc & Enter::
if(global DESKIDX == 1){
    Send, ^#{Right}
    global DESKIDX = 2
}
else{
    Send, ^#{Left}
    global DESKIDX = 1
}
return

SwitchIME(dwLayout){
    HKL:=DllCall("LoadKeyboardLayout", Str, dwLayout, UInt, 1)
    ControlGetFocus,ctl,A
    SendMessage,0x50,0,HKL,%ctl%,A
}

#If WinActive("ahk_exe SumatraPDF.exe")
{
    e::send, {Up}{Up}{Up}{Up}
    u::send, {Down}{Down}{Down}{Down}
    n::send, ^+{Tab}
    i::send, ^{Tab}
    +e::send, {PgUp}
    +u::send, {PgDn}
}
#IF

#If WinActive("ahk_exe 全球学术快报.exe")
{
    ; e::send, {Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}
    ; n::send, {Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}
    ; u::send, {Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}
    ; i::send, {Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}
    ; +e::send, {PgUp}
    ; +u::send, {PgDn}
}
#IF

#If WinActive("ahk_exe CAJVieweru.exe")
{
    e::send, {Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}
    n::send, {Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}
    u::send, {Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}
    i::send, {Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}
    +e::send, {PgUp}
    +u::send, {PgDn}
}
#IF

#If WinActive("ahk_exe msedge.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
    !q::send, ^w
    !n::send, ^+{Tab}
    !i::send, ^{tab}
    !Enter::send, {F11}
}
#IF

#If WinActive("ahk_exe chrome.exe")
{
    !q::send, ^w
    !n::send, ^+{Tab}
    !i::send, ^{tab}
    !Enter::send, {F11}
    Esc:: send, {Escape}
}
#IF

#If WinActive("ahk_exe Code.exe")
{
    ; Esc & l::
    ; send, !xs
    ; send, !xp
    ; send, {ESC}
    ; return
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    return

    Escape & b::^b
}
#IF

#If WinActive("ahk_class SunAwtFrame")
{
    ^t::send, !{F12}
    ; Esc::
    ;     send, {ESC}
    ;     SWITCHIME(04090409)
    ; return
}
#IF

#If WinActive("ahk_exe alacritty.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
}
#IF

#If WinActive("ahk_exe Wechat.exe")
{
    Esc::RETURN
    !q::Send, ^{Up}
    !w::Send, ^{Down}
}
#IF

#If WinActive("ahk_exe WechatStore.exe")
{
    Esc::RETURN
    !q::Send, ^{Up}
    !w::Send, ^{Down}
}
#IF

#If WinActive("ahk_exe Pal2_x64.exe")
{
    a::Click
}
#IF

#If WinActive("ahk_exe putty.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
}
#IF


#If WinActive("ahk_exe yuanbao.exe")
{
    ; 按 F1 向上滚动（相当于滚轮上滚）
    j::
        Send {WheelUp}
        Send {WheelUp}
        Send {WheelUp}
    return
    ; 按 F2 向下滚动（相当于滚轮下滚）
    k::
    Send {WheelDown}
    Send {WheelDown}
    Send {WheelDown}
    return
    Esc::
        sendinput, {ESC}
        SWITCHIME(04090409)
    return
}
#IF


#If WinActive("ahk_exe windowsTerminal.exe")
{
    Esc::
        sendinput, {ESC}
        SWITCHIME(04090409)
    return
}
#IF


global MYKEYTOGGLE := 0
#If WinActive("ahk_exe mhtab.exe")
{
    Esc::
        if(global MYKEYTOGGLE == 0) {
            global MYKEYTOGGLE = 1
        }else{
            global MYKEYTOGGLE = 0
        }
        send, {Esc}
    return
    1::
        if(global MYKEYTOGGLE == 0) {
            send, {f1}
        }else{
            send, 1
        }
    return
    t::
        if(global MYKEYTOGGLE == 0) {
            send, !g
        }else{
            send, d
        }
    return
    2::
        if(global MYKEYTOGGLE == 0) {
            send, {f2}
        }else{
            send, 2
        }
    return
    z::
        if(global MYKEYTOGGLE == 0) {
            send, !o
        }else{
            send, z
        }
    return
    f::
        if(global MYKEYTOGGLE == 0) {
            Click right
        }else{
            send, s
        }
    return
    d::
        if(global MYKEYTOGGLE == 0) {
            send, {LButton}
        }else{
            send, t
        }
    return
    a::
        if(global MYKEYTOGGLE == 0) {
            send, !e
        }else{
            send, a
        }
    return
    s::
        if(global MYKEYTOGGLE == 0) {
            send, !w
        }else{
            send, r
        }
    return
    e::
        if(global MYKEYTOGGLE == 0) {
            send, !q
        }else{
            send, f
        }
    return
    x::
        if(global MYKEYTOGGLE == 0) {
            send, !h
            send, {f9}
        }else{
            send, x
        }
    return
    g::
        if(global MYKEYTOGGLE == 0) {
            send, !g
        }else{
            send, g
        }
    return
    Space::send, {Space}
    Space & f::!e
    Space & w::!w
    Space & q::!q
    Space & a::!a
}
#IF

; Capslock
; Capslock::Escape
!F9::
    WinGetPos, X, Y, W, H, A
    MsgBox, A is at %X%`,%Y% and its size is %W%x%H%
    ; if(X<=0 && Y <= 0) {
    ;     Send, #{Down}
    ; }

F10::
    taken := []
    input := "arstarstarst"
    Loop, Parse, input
    {
        msgbox, %A_LoopField%
        taken[A_LoopField] := (StrLen(taken[A_LoopField])?taken[A_LoopField]+1:1)
    }

call_first_edge() {
    call_app("msedge.exe")
}

call_wechat() {
    id := WinExist("ahk_exe WeChat.exe")
    if (id > 0)
    {
        WinActivate, ahk_id %id%
    }
    else {
        send, #4
        id := WinExist("ahk_exe Code.exe")
    }
    return
}

call_first_vscode() {
    id := WinExist("ahk_exe Code.exe")
    if (id > 0)
    {
        WinActivate, ahk_id %id%
    }
    else {
        send, #3
        id := WinExist("ahk_exe Code.exe")
    }
    return
}

call_wt() {
    wt_id := WinExist("ahk_exe WindowsTerminal.exe")
    if (wt_id > 0)
    {
        WinActivate, ahk_id %wt_id%
    }
    else {
        run wt
        wt_id := WinExist("ahk_exe WindowsTerminal.exe")
    }
    return
}

call_alacritty() {
    call_hide_app("alacritty.exe")
}

call_hide_app(app_name) {
    id := WinExist("ahk_exe" app_name)
    if (id > 0)
    {
        WinActivate, ahk_id %id%
    }
    else {
        ; msgbox, hello
        run, %app_name%,,hide
        id := WinExist("ahk_exe" app_name)
    }
    return
}

call_app(app_name) {
    id := WinExist("ahk_exe" app_name)
    if (id > 0)
    {
        WinActivate, ahk_id %id%
    }
    else {
        run, %app_name%
        id := WinExist("ahk_exe" app_name)
    }
    return
}

call_tiny_wt() {
    static wt_tiny_id := -1
    if (static wt_tiny_id == -1)
    {
        Send, #6
        wt_tiny_id := WinExist("ahk_exe WindowsTerminal.exe")
    }
    else {
        WinActivate, ahk_id %wt_tiny_id%
    }
    return
}
